/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dalobject;

import dalInterface.DALAccount;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import utils.DataTable;

/**
 *
 * @author salm
 */
public class DALAccountImpl implements DALAccount {

    private final Connection sqlConnection;

    public DALAccountImpl(Connection connect) throws RemoteException {
        super();
        this.sqlConnection = connect;
    }
    
    @Override
    public DataTable getAccount(String userName) throws RemoteException
    {
        synchronized (sqlConnection)
        {
            // NEED TABLE
            try {
                Statement st = sqlConnection.createStatement();
                String queryString = String.format("SELECT * FROM ACCOUNT WHERE UserID=\'%s\'", userName);

                return DataTable.createDataTable(st.executeQuery(queryString), "UserID", "UserName", "Experience", "Level");
            } catch (SQLException ex) {
                Logger.getLogger(DALAccountImpl.class.getName()).log(Level.SEVERE, null, ex);
            }

            return null;
        }
    }

    @Override
    public DataTable getAccount(String userName, String password) throws RemoteException
    {
        synchronized (sqlConnection)
        {
            // NEED TABLE
            try {
                Statement st = sqlConnection.createStatement();
                String queryString = String.format("SELECT * FROM ACCOUNT WHERE UserID=\'%s\' AND Password=\'%s\'", userName, password);

                return DataTable.createDataTable(st.executeQuery(queryString), "UserID", "UserName", "Experience", "Level");
            } catch (SQLException ex) {
                Logger.getLogger(DALAccountImpl.class.getName()).log(Level.SEVERE, null, ex);
            }

            return null;
        }
    }

    @Override
    public boolean isAccountExist(String userName) throws RemoteException
    {
        synchronized (sqlConnection)
        {
            try {
                Statement st = sqlConnection.createStatement();
                String queryString = String.format("SELECT Name FROM ACCOUNT WHERE Name=\'%s\'", userName);
                ResultSet rs = st.executeQuery(queryString);

                return rs.next();
            } catch (SQLException ex) {
                Logger.getLogger(DALAccountImpl.class.getName()).log(Level.SEVERE, null, ex);
            }

            return false;
        }
    }

    @Override
    public boolean insertAccount(String userName, String password, String name) throws RemoteException
    {
        synchronized (sqlConnection)
        {
            try {
                Statement st = sqlConnection.createStatement();
                String queryString = String.format("INSERT INTO ACCOUNT VALUES (\'%s\', \'%s\', \'%s\', 0, 1, 0)", userName, name, password);

                st.execute(queryString);
                return true;
            } catch (SQLException ex) {
                Logger.getLogger(DALAccountImpl.class.getName()).log(Level.SEVERE, null, ex);
            }

            return false;
        }
    }    

    @Override
    public DataTable getAccountInfo(String userName) throws RemoteException 
    {
        synchronized (sqlConnection)
        {
            try {
                Statement st = sqlConnection.createStatement();
                String queryString = String.format("SELECT * FROM ACCOUNT WHERE UserID=\'%s\'", userName);

                return DataTable.createDataTable(st.executeQuery(queryString), "UserID", "UserName", "Experience", "Level", "TypeID");
            } catch (SQLException ex) {
                Logger.getLogger(DALAccountImpl.class.getName()).log(Level.SEVERE, null, ex);
            }

            return null;
        }
    }

    @Override
    public void updateAccountExp(String userName, long newExp) throws RemoteException
    {
        synchronized (sqlConnection)
        {
            try {
                Statement st = sqlConnection.createStatement();
                String queryString = String.format("UPDATE ACCOUNT SET Experience = %ld WHERE UserID=\'%s\'", newExp , userName);

                st.executeUpdate(queryString);
            } catch (SQLException ex) {
                Logger.getLogger(DALAccountImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    @Override
    public boolean changePassword(String userName, String oldPassword, String newPassword) throws RemoteException {
        synchronized (sqlConnection)
        {
            try {
                Statement st = sqlConnection.createStatement();
                String queryString = String.format("UPDATE ACCOUNT SET Password "
                        + "= \'%s\' WHERE UserID=\'%s\' AND Password = \'%s\'",
                        newPassword , userName, oldPassword);
                
                int result = st.executeUpdate(queryString);
                return result > 0;
            } catch (SQLException ex) {
                Logger.getLogger(DALAccountImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            return false;
        }
    }
}
